home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-07-25 | 12.7 KB | 485 lines | [TEXT/PJMM] |
- { ******************************************************** }
- { "myAppleEvents.p" }
- { }
- { by John A. Love, III [ Washington Apple Pi Users' Group] }
- { }
- { using Symantec's "THINK Lightspeed Pascal", v 3.02 }
- { < based on Eric Soldan's "CShell" > }
- { }
- { ******************************************************** }
-
-
- UNIT myAppleEvents;
-
- INTERFACE
-
- USES
- Types, Memory, OSUtils, Quickdraw, Events, Files, AppleTalk, PPCToolbox, Processes, EPPC, Notification, AppleEvents, AERegistry, Script, Packages, Dialogs, CTBUtilities, Connections, GestaltEqu, wBMInterface, wBMGlobals, wBMMiscSubs, wBMWindSubs;
-
-
- FUNCTION AppleEventsActive: BOOLEAN;
- FUNCTION PPCToolboxActive: BOOLEAN;
- FUNCTION AcceptHLEvent: OSErr;
- FUNCTION MissedAnyParameters (event: EventRecord; theAEEvent: AppleEvent): OSErr;
- FUNCTION InitPPCToolbox: OSErr;
- FUNCTION DoAEOpenApplication (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
- FUNCTION DoAEOpenDocuments (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSerr;
- FUNCTION DoAEPrintDocuments (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
- FUNCTION DoAEQuitApplication (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
- FUNCTION DoAEMoveWindow (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
- FUNCTION InitAppleEvents: OSErr;
- PROCEDURE CreateAndSendAE (class: AEEventClass; id: AEEventID);
- PROCEDURE DoHighLevelEvent (event: EventRecord);
-
- TYPE
- triplets = RECORD
- theEventClass: AEEventClass;
- theEventID: AEEventID;
- theHandler: ProcPtr;
- END;
-
- CONST
- _Gestalt = $A1AD;
- noAppleEventsErr = -10000;
- noPPCToolboxErr = -15000;
- AEAlertID = 777;
- kCustomEventClass = 'CUST';
- kMoveWindClass = kCustomEventClass;
- kMoveWindID = aeMove;
-
- VAR
- myFeature: LONGINT;
- gHasAppleEvents, gHasPPCToolbox: BOOLEAN;
- keywordsToInstall: ARRAY[0..4] OF triplets;
- err: OSErr;
- returnString: Str255;
- itemNbr: INTEGER;
- theClient: TargetID;
- clientRefcon: LONGINT;
- currScreenDev, nextScreenDev: GDHandle;
-
-
-
-
- IMPLEMENTATION
-
-
-
-
- FUNCTION AppleEventsActive: BOOLEAN;
- { Do we have a machine on which the Apple Events Manager exists ??? }
-
- BEGIN
-
- AppleEventsActive := FALSE;
-
- IF TrapAvailable(_Gestalt) THEN
- IF Gestalt(gestaltAppleEventsAttr, myFeature) = noErr THEN
- IF BitTst(@myFeature, 31 - gestaltAppleEventsPresent) THEN
- AppleEventsActive := TRUE;
-
- END; { AppleEventsActive }
-
-
-
- FUNCTION PPCToolboxActive: BOOLEAN;
- { Do we have a machine on which the PPCToolbox exists ??? }
-
- BEGIN
-
- PPCToolboxActive := FALSE;
-
- IF TrapAvailable(_Gestalt) THEN
- IF Gestalt(gestaltPPCToolboxAttr, myFeature) = noErr THEN
- IF BitTst(@myFeature, 31 - gestaltPPCToolboxPresent) THEN
- PPCToolboxActive := TRUE;
-
- END; { PPCToolboxActive }
-
-
-
- FUNCTION AcceptHLEvent: OSErr;
-
- VAR
- myBuff: Ptr;
- myLen: LONGINT;
-
- BEGIN
-
- myLen := 0; { Start with zero to allow _AcceptHighLevelEvent }
- myBuff := NIL; { to determine the size. }
-
- err := AcceptHighLevelEvent(theClient, clientRefcon, myBuff, myLen);
- ;
- IF err = bufferIsSmall THEN
- BEGIN
- myBuff := NewClearPtr(myLen);
- err := AcceptHighLevelEvent(theClient, clientRefcon, myBuff, myLen);
- END;
-
- AcceptHLEvent := err;
-
- END; { AcceptHLEvent }
-
-
-
- FUNCTION MissedAnyParameters (event: EventRecord; theAEEvent: AppleEvent): OSErr;
-
- VAR
- ignoredActualType: DescType;
- missedKeyword: AEKeyword;
- ignoredActualSize: Size;
-
- BEGIN
-
- err := AEGetAttributePtr(theAEEvent, keyMissedKeywordAttr, typeKeyword, ignoredActualType, @missedKeyword, SizeOf(missedKeyword), ignoredActualSize);
-
- IF err = noErr THEN { We found some unused parameters. }
- BEGIN
- event.message := LONGINT(ignoredActualType);
- event.where := Point(LONGINT(missedKeyword));
- err := errAEEventNotHandled;
- END { noErr }
- ELSE IF err = errAEDescNotFound THEN { No more. }
- err := noErr;
-
- MissedAnyParameters := err;
-
- END; { MissedAnyParameters }
-
-
-
- FUNCTION InitPPCToolbox: OSErr;
-
- BEGIN
-
- gHasPPCToolbox := PPCToolboxActive;
-
- IF gHasPPCToolbox THEN
- BEGIN
-
- IF BAND(myFeature, gestaltPPCSupportsRealTime) = 0 THEN
- BEGIN
- err := PPCInit;
- IF err = noErr THEN
- err := Gestalt(gestaltPPCToolboxAttr, myFeature);
- END
- ELSE { does NOT need initialization }
- err := noErr;
-
- END
- ELSE
- err := noPPCToolboxErr;
-
- InitPPCToolbox := err;
-
- END; { InitPPCToolbox }
-
-
-
- PROCEDURE DisplayClientTypeAndCreator (theAEEvent: AppleEvent);
-
- VAR
- nameStr: Str32;
- senderType, senderCreator: INTEGER;
- typeStr, creatorStr: Str255;
- returnedType: DescType;
- sourceOfAE: TargetID;
- actualSize: Size;
-
- BEGIN
-
- IF gInitPPCToolbox = noErr THEN
- BEGIN
-
- err := AEGetAttributePtr(theAEEvent, keyAddressAttr, typeTargetID, returnedType, @sourceOfAE, SizeOf(sourceOfAE), actualSize);
- IF err = noErr THEN
- BEGIN
- WITH sourceOfAE.name DO
- BEGIN
-
- nameStr := name;
- IF portKindSelector = ppcByCreatorAndType THEN
- BEGIN
- NumToString(LONGINT(portType), typeStr);
- NumToString(LONGINT(portCreator), creatorStr);
- END
- ELSE { ppcByString }
- BEGIN
- typeStr := copy(portTypeStr, 1, 4); { Skip the length byte. }
- creatorStr := copy(portTypeStr, 5, 4);
- END;
-
- END; { WITH }
-
- ParamText(concat('Client name: ‘', concat(nameStr, '’')), concat('Client type: ‘', concat(typeStr, '’')), concat('Client creator: ‘', concat(creatorStr, '’')), '');
- itemNbr := NoteAlert(AEAlertID, NIL);
- END; { noErr }
-
- END; { gInitPPCToolbox }
-
- END; { DisplayClientTypeAndCreator }
-
-
-
- FUNCTION DoAEOpenApplication (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
-
- BEGIN
-
- ParamText('wBarDemo OBVIOUSLY!!', 'supports AppleEvent =', 'Open Application', '');
- itemNbr := NoteAlert(AEAlertID, NIL);
- DoAEOpenApplication := MissedAnyParameters(Event, message);
- ;
- { Done automatically by Apple Event Manager: }
- { DoAEOpenApplication := AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, @err, SizeOf(err)); }
-
- DisplayClientTypeAndCreator(message);
-
- END; { DoAEOpenApplication }
-
-
-
- FUNCTION DoAEOpenDocuments (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSerr;
-
- BEGIN
-
- ParamText('wBarDemo does NOT support', 'AppleEvent =', 'Open Documents', '');
- itemNbr := StopAlert(AEAlertID, NIL);
- DoAEOpenDocuments := errAEEventNotHandled;
- ;
- { DoAEOpenDocuments := AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, @err, SizeOf(err)); }
-
- returnString := 'wBarDemo does NOT support AppleEvent = Open Documents';
- err := AEPutParamPtr(reply, keyErrorString, typeChar, @returnString[1], length(returnString));
-
- END; { DoAEOpenDocuments }
-
-
-
- FUNCTION DoAEPrintDocuments (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
-
- BEGIN
-
- ParamText('wBarDemo does NOT support', 'AppleEvent =', 'Print Documents', '');
- itemNbr := StopAlert(AEAlertID, NIL);
- DoAEPrintDocuments := errAEEventNotHandled;
- ;
- { DoAEPrintDocuments := AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, @err, SizeOf(err)); }
-
- returnString := 'wBarDemo does NOT support AppleEvent = Print Documents';
- err := AEPutParamPtr(reply, keyErrorString, typeChar, @returnString[1], length(returnString));
-
- END; { DoAEPrintDocuments }
-
-
-
- FUNCTION DoAEQuitApplication (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
-
- BEGIN
-
- Done := TRUE;
- ParamText('and NO doubt whatSOever!!', 'about AppleEvent =', 'Quit Application', '');
- itemNbr := NoteAlert(AEAlertID, NIL);
- DoAEQuitApplication := MissedAnyParameters(Event, message);
- ;
- { DoAEQuitApplication := AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, @err, SizeOf(err)); }
-
- DisplayClientTypeAndCreator(message);
-
- END; { DoAEQuitApplication }
-
-
-
- FUNCTION DoAEMoveWindow (message: AppleEvent; reply: AppleEvent; refcon: LONGINT): OSErr;
-
- VAR
- returnedType: DescType;
- movePoint: Point;
- actualSize: Size;
-
-
- BEGIN
-
- err := AEGetParamPtr(message, keyDirectObject, typeQDPoint, returnedType, @movePoint, SizeOf(movePoint), actualSize);
- IF err = noErr THEN
- BEGIN
- MoveWindow(FrontWindow, movePoint.h, movePoint.v, true);
- err := MissedAnyParameters(Event, message);
- END;
- ;
- DoAEMoveWindow := err;
-
- END; { DoAEMoveWindow }
-
-
-
- FUNCTION InitAppleEvents: OSErr;
-
- VAR
- i: INTEGER;
-
- BEGIN
-
- gHasAppleEvents := AppleEventsActive;
-
- IF gHasAppleEvents THEN
- BEGIN
- WITH keywordsToInstall[0] DO
- BEGIN
- theEventClass := kCoreEventClass;
- theEventID := kAEOpenApplication;
- theHandler := EventHandlerProcPtr(@DoAEOpenApplication);
- END;
- ;
- WITH keywordsToInstall[1] DO
- BEGIN
- theEventClass := kCoreEventClass;
- theEventID := kAEOpenDocuments;
- theHandler := EventHandlerProcPtr(@DoAEOpenDocuments);
- END;
- ;
- WITH keywordsToInstall[2] DO
- BEGIN
- theEventClass := kCoreEventClass;
- theEventID := kAEPrintDocuments;
- theHandler := EventHandlerProcPtr(@DoAEPrintDocuments);
- END;
- ;
- WITH keywordsToInstall[3] DO
- BEGIN
- theEventClass := kCoreEventClass;
- theEventID := kAEQuitApplication;
- theHandler := EventHandlerProcPtr(@DoAEQuitApplication);
- END;
- ;
- WITH keywordsToInstall[4] DO
- BEGIN
- theEventClass := kMoveWindClass;
- theEventID := kMoveWindID;
- theHandler := EventHandlerProcPtr(@DoAEMoveWindow);
- END;
-
- i := 0;
- REPEAT
- err := AEInstallEventHandler(keywordsToInstall[i].theEventClass, keywordsToInstall[i].theEventID, keywordsToInstall[i].theHandler, 0, false);
- i := i + 1;
- UNTIL (i > 4) | (err <> noErr);
-
- InitAppleEvents := err;
- END { gHasAppleEvents }
-
- ELSE
- InitAppleEvents := noAppleEventsErr;
-
- END; { InitAppleEvents }
-
-
-
- PROCEDURE CreateAndSendAE (class: AEEventClass; id: AEEventID);
- { Given an event ID, this routine creates such an event and posts it with }
- { AESend. Right now, all we know about are our own MoveWindow events. }
-
- CONST
- PRIORITY = kAENormalPriority;
-
- VAR
-
- err: OSErr;
- me: ProcessSerialNumber;
- theAevt: AppleEvent;
- thisApp: AEAddressDesc;
- theReply: AppleEvent;
- thePoint: Point;
- window: WindowPtr;
- windRect, screen: Rect;
- offsetForTitleBar, screenH, screenV, rand, offH, offV: INTEGER;
-
-
- BEGIN
-
- window := FrontWindow;
- IF window = NIL THEN
- EXIT(CreateAndSendAE);
-
- windRect := window^.portRect;
- LocalGlobal(windRect);
- offsetForTitleBar := windRect.top - 1 - WindowPeek(window)^.strucRgn^^.rgnBBox.top;
-
- WITH me DO
- BEGIN
- highLongOfPSN := 0;
- lowLongOfPSN := kCurrentProcess;
- END; { WITH }
- ;
- err := AECreateDesc(typeProcessSerialNumber, @me, SizeOf(me), thisApp);
- IF err = noErr THEN
- IF id = kMoveWindID THEN
- { Create the Window moved event. This is just a random point passed to the }
- { receiving application, which is expected to move the frontmost window. }
- BEGIN
-
- IF aMac2 THEN
- BEGIN
-
- WHILE nextScreenDev <> NIL DO
- BEGIN
- IF TestDeviceAttribute(nextScreenDev, screenDevice) & TestDeviceAttribute(nextScreenDev, screenActive) THEN
- Leave { WHILE loop }
- ELSE
- nextScreenDev := GetNextDevice(nextScreenDev);
- END; { WHILE }
- ;
- IF nextScreenDev <> NIL THEN
- currScreenDev := nextScreenDev
- ELSE
- currScreenDev := GetMainDevice; { Restart at the beginning. }
- screen := currScreenDev^^.gdRect;
- nextScreenDev := GetNextDevice(currScreenDev);
-
- END { aMac2 }
- ELSE
- screen := screenBits.bounds;
-
- WITH screen DO
- BEGIN
- { InsetRect(screen, 10, 10); }
- screenH := right - left - 20;
- screenV := bottom - top - 20;
- rand := Random;
- offH := left + 10;
- offV := top + mBarHt + 10 + offsetForTitleBar;
- SetPt(thePoint, offH + abs(rand MOD screenH), offV + abs(rand MOD screenV));
- END; { WITH }
-
- err := AECreateAppleEvent(class, id, thisApp, kAutoGenerateReturnID, kAnyTransactionID, theAevt);
-
- IF err = noErr THEN
- { Things are still cool... }
- err := AEPutParamPtr(theAevt, keyDirectObject, typeQDPoint, @thePoint, SizeOf(Point));
-
- IF err = noErr THEN
- err := AESend(theAevt, theReply, kAENoReply, PRIORITY, 0, NIL, NIL);
-
- err := AEDisposeDesc(thisApp);
-
- END; { kMoveWindID }
-
- END; { CreateAndSendAE }
-
-
-
- PROCEDURE DoHighLevelEvent (event: EventRecord);
- { Simply calls AEProcessAppleEvent and reports any errors to the client. }
- { AEProcessAppleEvent looks in its table of registered events and sees if }
- { the current event is registered. If so, it calls the routine associated }
- { with that event. }
-
- BEGIN
- IF gInitAppleEvents = noErr THEN
- err := AEProcessAppleEvent(event);
- END; { DoHighLevelEvent }
-
-
-
-
- END. { UNIT = myAppleEvents }